package com.changgou.goods.controller;

import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.exception.CustomException;
import com.changgou.goods.pojo.Para;
import com.changgou.goods.service.ParaService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/para")
public class ParaController {
    @Autowired
    private ParaService paraService;

    /**
     * 新增参数
     * @param para
     * @return
     */
    @PostMapping("/add_para")
    public Result add_para(@RequestBody Para para){       try {
        paraService.add_para(para);
        return new Result(true, StatusCode.OK,"添加参数成功！");
    }catch (Exception e){
        e.printStackTrace();
    }
        return new Result(true,StatusCode.ERROR,"添加参数失败！");
    }

    /**
     * 删除参数
     * @param id
     * @return
     */
    @GetMapping("/delete_para/{id}")
    public Result delete_para(@PathVariable Integer id){
        int count = paraService.delete_para(id);
        if (count>0) {
            return new Result(true, StatusCode.OK, "删除参数成功！");
        }else {
            return new Result(true, StatusCode.ERROR,"删除参数失败！");
        }
    }

    @GetMapping(value = "/findAllPara/{page}/{size}" )
    public Result findPage( @PathVariable  int page, @PathVariable  int size) throws Exception {
        Page<Para> pageList = paraService.findPage(page, size);
        if(pageList.size()==0){
            throw new CustomException(StatusCode.MESSAGE_BACD,"您查询的数据暂无，请联系管理核对！！！");
        }
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }
}
